//
//  PhotoBrowerViewController.swift
//  weibo
//
//  Created by 叶子韬韬 on 2017/5/13.
//  Copyright © 2017年 叶子韬韬. All rights reserved.
//

import UIKit
import SnapKit
internal let PhotoCell = "PhotoCell"
class PhotoBrowerViewController: UIViewController {

    var indexPath:NSIndexPath?
    var picURLs:[URL]?
    
    internal lazy var collectionView:UICollectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: PhotoBrowerCollectionViewFlow())
    
    internal lazy var closeBtn:UIButton = UIButton(backGroundColor: UIColor.darkGray, font: UIFont.systemFont(ofSize: 15), title: "关 闭")
    
    internal lazy var saveBtn:UIButton = UIButton(backGroundColor: UIColor.darkGray, font: UIFont.systemFont(ofSize: 15), title: "保 存")
    
    init(indexPath:NSIndexPath,picURLs:[URL]) {
        
        super.init(nibName: nil, bundle: nil)
        
        self.indexPath = indexPath
        self.picURLs = picURLs
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.purple
        
        setupUI()
        
    }



}

extension PhotoBrowerViewController{
    internal func closeClick(){
        dismiss(animated: true, completion: nil)
    }
}

// MARK:- #注释>设置UI
extension PhotoBrowerViewController{
 
    internal func setupUI(){
        view.addSubview(collectionView)
        view.addSubview(closeBtn)
        view.addSubview(saveBtn)
        
        collectionView.frame = view.bounds
        closeBtn.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.bottom.equalTo(-20)
            make.size.equalTo(CGSize.init(width: 90, height: 32))
        }
        saveBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-20)
            make.bottom.equalTo(closeBtn)
            make.size.equalTo(closeBtn.snp.size)
        }
        collectionView.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: PhotoCell)
        collectionView.dataSource = self
        
        closeBtn.addTarget(self, action: #selector(PhotoBrowerViewController.closeClick), for: .touchUpInside)
        
    }
    
    
    
}
extension PhotoBrowerViewController:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picURLs!.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhotoCell, for: indexPath) as! PhotoCollectionViewCell
        
//        cell.backgroundColor = ((indexPath.item % 2) == 0 ? UIColor.red : UIColor.purple)
        
        cell.picUrl = picURLs![indexPath.item]
        
        return cell
        
    }
}

class PhotoBrowerCollectionViewFlow: UICollectionViewFlowLayout {
    override func prepare() {
        itemSize = collectionView!.frame.size
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = .horizontal
        
        collectionView?.isPagingEnabled = true
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
}
